package com.uestc.soft.service;

import com.uestc.soft.mapper.UserMapper;
import com.uestc.soft.pojo.User;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {      // 业务逻辑层    所有的功能实现均在Service中实现，Controller只提供通信信道

    @Resource
    private UserMapper userMapper;

    public Map<String, Object> login(User user) {
        String password = user.getPassword();
        byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
        String passwordMD5 = DigestUtils.md5DigestAsHex(bytes);     //对密码password进行MD5加密
        user.setPassword(passwordMD5);

//        String black = user.getBlacklist();

        User loginUser = userMapper.login(user);
        Map<String, Object> map = new HashMap<>();   // 初始化一个返回map数据对象
        if(loginUser == null) {   // 代表用户名不存在  或者该用户在黑名单中
            map.put("code", 50001);
            map.put("msg", "用户名或者密码错误");
        }
        else if(loginUser.getBlacklist().equals("yes")){
            map.put("code", 50001);
            map.put("msg", "登录失败，用户名在黑名单中");
        }
        else {                    // 代表用户登录成功 用户名和密码正确
            loginUser.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
            userMapper.updateLastLoginTime(loginUser);   // 更新最后的登录时间

            map.put("code", 200);
            map.put("msg", "登录成功");
            map.put("user", loginUser);
        }
        return map;
    }
    public Map<String, Object> selectByPage(int page, int limit, String username, String age) {
        // page 页码   (page-1)*limit=起始下标index
        int index = (page - 1) * limit;   // 计算出起始下标
        List<User> userList = userMapper.selectByPage(index, limit, username, age);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", userMapper.selectCount(username, age));
        map.put("data", userList);
        return map;
    }

    public Map<String, Object> insert(User user) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        // 写入一个注册时间 System.currentTimeMillis()代表当前服务器系统时间
        user.setRegTime(timestamp);

        String password = user.getPassword();
        byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
        String passwordMD5 = DigestUtils.md5DigestAsHex(bytes);//对密码进行MD5加密
        user.setPassword(passwordMD5);

        String blacklist = "no";
        user.setBlacklist(blacklist);   //新增用户默认在白名单中

        userMapper.insert(user);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增用户成功");
        return map;
    }
    public Map<String, Object> deleteById(Integer id) {
        userMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        return map;
    }

    public Map<String, Object> updateAge(Integer id, Integer age) {//修改用户的年龄
        userMapper.updateAge(id, age);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新用户成功");
        return map;
    }
    public Map<String, Object> deleteBlacklist(Integer id) {
        userMapper.deleteBlacklist(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功从黑名单中移除");
        return map;
    }
    public Map<String, Object> insertBlacklist(Integer id) {
        userMapper.insertBlacklist(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功从黑名单中移除");
        return map;
    }
    public Map<String, Object> selectByBlacklist(int page, int limit, String username, String age, String blacklist) {
        // page 页码   (page-1)*limit=起始下标index
        int index = (page - 1) * limit;   // 计算出起始下标
        List<User> userList = userMapper.selectByBlacklist(index, limit, username, age, blacklist);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", userMapper.selectBlacklist(blacklist));
        map.put("data", userList);
        return map;
    }
}

